package com.leather.express.vo;

import com.leather.common.entity.Express;
import com.leather.common.result.CodeMsg;
import com.leather.common.result.Result;
import com.leather.common.toolkits.DateKit;
import com.leather.common.toolkits.ValidateKit;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;

@Data
public class ExpressVo implements Serializable {

    private String getAddress;

    private double price;

    private Integer userId;


    private Integer expressAddress;

    private String type;

    private String phone;

    private String code;

    private String name;

    public Express transfer(){
        Express express = new Express();
        express.setId(0);
        express.setUserId(userId);
        express.setExpressAddress(expressAddress);
        express.setGetAddress(getAddress);
        express.setCreateTime(DateKit.now());
        express.setPrice(price);
        express.setFlag(0);
        express.setGetFlag(0);
        express.setType(type);
        express.setPhone(phone);
        express.setCode(code);
        express.setName(name);
        return express;
    }

    public Result validate(){
        if(StringUtils.isBlank(getAddress)) {
            return Result.error(CodeMsg.EMPTY_GET_ADDRESS);
        }
        if(StringUtils.isBlank(type)) {
            return Result.error(CodeMsg.EMPTY_TYPE);
        }
        if(StringUtils.isBlank(phone)) {
            return Result.error(CodeMsg.EMPTY_PHONE);
        }
        if(!ValidateKit.isMobile(phone)) {
            return Result.error(CodeMsg.ERROR_PHONE);
        }
        if(StringUtils.isBlank(code)) {
            return Result.error(CodeMsg.EMPTY_CODE);
        }
        if(StringUtils.isBlank(name)) {
            return Result.error(CodeMsg.EMPTY_NAME);
        }
        if(price<0) {
            return Result.error(CodeMsg.ERROR_PRICE);
        }
        if(userId<=0) {
            return Result.error(CodeMsg.ERROR_USER_ID);
        }
        if(expressAddress<0) {
            return Result.error(CodeMsg.EMPTY_EXPRESS_ADDRESS);
        }
        return Result.success(transfer());
    }
}
